package itbour.onetouchshow.bean.inching.noppt;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import itbour.onetouchshow.utils.L_;

/**
 * Created by zjl_d on 2017/11/22.
 */

public class NoPPTRootTableValueV10Bean implements Parcelable {

    public static final Parcelable.Creator<NoPPTRootTableValueV10Bean> CREATOR = new Parcelable.Creator<NoPPTRootTableValueV10Bean>() {
        @Override
        public NoPPTRootTableValueV10Bean createFromParcel(Parcel source) {
            return new NoPPTRootTableValueV10Bean(source);
        }

        @Override
        public NoPPTRootTableValueV10Bean[] newArray(int size) {
            return new NoPPTRootTableValueV10Bean[size];
        }
    };
    public int ct;
    public int type;
    public int id;
    public int minRow;
    public int maxRow;
    public int minCol;
    public int maxCol;
    public List<RootTableValueV10RowBean> children;


    public NoPPTRootTableValueV10Bean() {
    }


    protected NoPPTRootTableValueV10Bean(Parcel in) {
        this.ct = in.readInt();
        this.type = in.readInt();
        this.id = in.readInt();
        this.minRow = in.readInt();
        this.maxRow = in.readInt();
        this.minCol = in.readInt();
        this.maxCol = in.readInt();
        this.children = new ArrayList<RootTableValueV10RowBean>();
        in.readList(this.children, RootTableValueV10RowBean.class.getClassLoader());
    }

    /**
     * 获取图表编辑后的二维数组
     */
    public ArrayList<ArrayList<String>> getResultStringArra() {
        ArrayList<ArrayList<String>> dataArra = new ArrayList<>();
        for (int i = 0; i < children.size(); i++) {
            RootTableValueV10RowBean subbean = children.get(i);
            ArrayList<String> temptArra = new ArrayList<>();
            for (int j = 0; j < subbean.children.size(); j++) {
                RootTableValueV10RowBean.RootTableValueV10ColumnBean subsubBean = subbean.children.get(j);
                temptArra.add(subsubBean.children.get(0).getValue());
            }
            dataArra.add(temptArra);
        }
        return dataArra;
    }

    /**
     * 刷新表格数据源, 给所有 cell 中的文本设置 rowType
     */

    public void refreshCellTextRowType() {
        for (int i = 0; i < children.size(); i++) {
            RootTableValueV10RowBean row = children.get(i);
            for (int j = 0; j < row.children.size(); j++) {
                RootTableValueV10RowBean.RootTableValueV10ColumnBean cell = row.children.get(j);
                cell.children.get(0).rowType = row.rowType;
            }
        }
    }

    /**
     * 删除指定行
     *
     * @param rawPosition
     */
    public void removeRawPosition(int rawPosition) {

        if (children.size() <= 1 || rawPosition >= children.size()) {
            L_.i("removeRowErr: currentRowSize: " + children.size() + " removeIndex: " + rawPosition);
            return;
        }
        // 当删除第 零行的时候, 可能为表头, 进行表头转换处理
        if (rawPosition == 0) {
            // 执行删除行操作, 至少要有两行才能删除
            RootTableValueV10RowBean titleRow = children.get(0);
            RootTableValueV10RowBean nextRow = children.get(1);
            nextRow.rowType = titleRow.rowType;

            for (int i = 0; i < nextRow.children.size(); i++) {
                RootTableValueV10RowBean.RootTableValueV10ColumnBean cell = nextRow.children.get(i);
                cell.children.get(0).rowType = nextRow.rowType;
            }
        }
        children.remove(rawPosition);
    }

    /**
     * 删除指定列
     *
     * @param columnsPosition
     */
    public void removeColmnsPosition(int columnsPosition) {
        int rawSize = children.size();
        int columnsSize = children.get(0).children.size();
        for (int rawCount = 0; rawCount < rawSize; rawCount++) {
            for (int columnsCount = 0; columnsCount < columnsSize; columnsCount++) {
                if (columnsCount == columnsPosition) {
                    children.get(rawCount).children.remove(columnsCount);
                }
            }
        }
    }

    /**
     * 指定位置增加行
     *
     * @param row
     */
    public void addRawPositon(Integer row) {
        // 异常保护
        if (row > children.size()) {
            L_.i("addRowError: currentSize:" + children.size() + "insertRow: " + row);
            return;
        }
        RootTableValueV10RowBean zeroData = children.get(0);
        RootTableValueV10RowBean designOrPrintTableBeanV54Row = copyZeroColmnsData(zeroData);

        // 存在表头, 且插入第零行, 交换表头标记
        if (row == 0 && zeroData.rowType == 1) {
            zeroData.rowType = 0;
            designOrPrintTableBeanV54Row.rowType = 1;
        }
        // 更新数据源 rowtype
        for (int i = 0; i < zeroData.children.size(); i++) {
            RootTableValueV10RowBean.RootTableValueV10ColumnBean zeroCell = zeroData.children.get(i);
            zeroCell.children.get(0).rowType = zeroData.rowType;

            RootTableValueV10RowBean.RootTableValueV10ColumnBean insertCell = designOrPrintTableBeanV54Row.children.get(i);
            insertCell.children.get(0).rowType = designOrPrintTableBeanV54Row.rowType;
        }

        children.add(row, designOrPrintTableBeanV54Row);

    }

    public void addColumnPosition(Integer column) {
        int rawSize = children.size();
        int columnsSize = children.get(0).children.size();
        // 异常保护, 超出表格最大值的保护在 UI 层处理, 此处只管添加
        if (column > columnsSize) {
            L_.i("currentColumnSize: " + columnsSize + " -- insertColumnIndex: " + column);
            return;
        }
        RootTableValueV10RowBean.RootTableValueV10ColumnBean designOrPrintTableBeanV54Column = children.get(0).children.get(0);
        for (int rawCount = 0; rawCount < rawSize; rawCount++) {

            Gson gson = new Gson();
            String orangleJson = gson.toJson(designOrPrintTableBeanV54Column);
            RootTableValueV10RowBean.RootTableValueV10ColumnBean copyColmnData =
                    gson.fromJson(orangleJson, RootTableValueV10RowBean.RootTableValueV10ColumnBean.class);
            // 清空默认值
            copyColmnData.id = 0;
            copyColmnData.children.get(0).value = "";
            copyColmnData.children.get(0).tip = "";

            // 加入数据源
            RootTableValueV10RowBean designOrPrintTableBeanV54Row = children.get(rawCount);
            List<RootTableValueV10RowBean.RootTableValueV10ColumnBean> children = designOrPrintTableBeanV54Row.children;
            L_.i("columnsCount===" + column + "column===" + column);

            // 设置对应数据源 的 rowtype
            copyColmnData.children.get(0).rowType = designOrPrintTableBeanV54Row.rowType;

            children.add(column, copyColmnData);
        }
    }

    /**
     * @param zeroData
     * @return
     */
    public RootTableValueV10RowBean copyZeroColmnsData(RootTableValueV10RowBean zeroData) {
        Gson gson = new Gson();
        String zeroJson = gson.toJson(zeroData);
        RootTableValueV10RowBean copy = gson.fromJson(zeroJson, RootTableValueV10RowBean.class);
        copy.rowType = 0;
        copy.id = 0;
        List<RootTableValueV10RowBean.RootTableValueV10ColumnBean> children = copy.children;
        int size = children.size();

        for (int i = 0; i < size; i++) {
            RootTableValueV10RowBean.RootTableValueV10ColumnBean copyColumn = children.get(i);
            copyColumn.id = 0;
            NoPPTRootTextValueV10Bean copyValue = copyColumn.children.get(0);
            copyValue.value = "";
            copyValue.tip = "";

        }
        return copy;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.ct);
        dest.writeInt(this.type);
        dest.writeInt(this.id);
        dest.writeInt(this.minRow);
        dest.writeInt(this.maxRow);
        dest.writeInt(this.minCol);
        dest.writeInt(this.maxCol);
        dest.writeList(this.children);
    }

    /**
     * 横着的
     */
    public static class RootTableValueV10RowBean {
        public int ct;
        public int type;
        public int id;
        //rowType 如果为1 那么代表表头  背景颜色变黑
        public int rowType;
        public List<RootTableValueV10ColumnBean> children;

        /**
         * ct : 815
         * type : 52
         * id : 8
         * children : [{"ct":808,"type":20,"id":9,"value":"默认文字","min":0,"max":4}]
         * 横着中的竖着每一个
         */

        public static class RootTableValueV10ColumnBean {
            public int ct;
            public int type;
            public int id;
            public List<NoPPTRootTextValueV10Bean> children;

        }


    }
}
